package server.multiServlet;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ThreadPoolExecutor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Connector implements LifeCycle {
    private Integer port;

    private ThreadPoolExecutor threadPoolExecutor;

    private ContainerBase containerBase;

    public Connector(Integer port) {
        this.port = port;
    }

    @Override
    public void init() {
        //启动监听
        Runnable task = () -> {
            try {
                ServerSocket serverSocket = new ServerSocket(port);
                while (true) {

                    Socket socket = serverSocket.accept();
                    RequestProcessor requestProcessor = new RequestProcessor(socket, containerBase);
                    threadPoolExecutor.execute(requestProcessor);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("=====>>>Minicat start on port：" + port);
        };

//        task.run();
        Thread thread = new Thread(task);
        thread.start();

    }
}
